#'###########################################
#'                                          #
#'  ------------------------------------    #
#'  |     COIN     __                  |    #
#'  |            >(' )                 |    #
#'  |              )/                  |    #
#'  |             /  `----/            |    #
#'  |             \  ~=- /             |    #
#'  |   ~^~^~^~^~^~^~^~^~^~^~^~^~^~^   |    #
#'  ------------------------------------    #
#'                                          #
#'###########################################

rm(list=ls())

#'#
#'# Chemins de travail
#'#
R_ROOT <- "C:/UQAR/Recherche/Maitrise/Donnees"
R_WORKING_DIRECTORY <- file.path(R_ROOT, "R")


#'#
#'# Chargement des dependances
#'# 
source(file.path(R_WORKING_DIRECTORY, "Utils/loadDependencies.R"))
DATA_PATH <- file.path(R_RESULTS_PATH, "HRCompare/post-grid")

hr.stats <- hr.files.load(DATA_PATH, type.level=c("raw", "LC3", "LC32", "LC321", "mb1", "mb2", "mb3"),
		h.levels=c("href", "500", "600", "800"))

#'#################################################################################################
#'#                                                                                              ##
#'#                                 MCP                                                          ##
#'#                                                                                              ##
#'#################################################################################################

hr.stats.mcp <- hr.stats[, list(sizeRatio=mean(sizeRatio.mcp.mean, na.rm=TRUE), sizeRatio.sd=sd(sizeRatio.mcp.mean, na.rm=TRUE),
				overlapIndex=mean(overlapIndex.mcp.mean, na.rm=TRUE), overlapIndex.sd=sd(overlapIndex.mcp.mean, na.rm=TRUE), n=mean(n.mean)), by=type]
hr.stats.mcp[, sizeRatio.lb := sizeRatio - sizeRatio.sd]
hr.stats.mcp[, sizeRatio.ub := sizeRatio + sizeRatio.sd]
hr.stats.mcp[, overlapIndex.lb := overlapIndex - overlapIndex.sd]
hr.stats.mcp[, overlapIndex.ub := overlapIndex + overlapIndex.sd]

#'#
#'# Graphiques
#'# 
cols <- cols <- grey(c(.9, .8, .7, .6, .5, .4, .3))

#png(file=file.path(DATA_PATH, "SizeRatio_mcp.png"), width=700, height=700)
barchart(sizeRatio ~ type, hr.stats.mcp, groups=NULL, ly=hr.stats.mcp$sizeRatio.lb,
		uy=hr.stats.mcp$sizeRatio.ub, prepanel=prepanel.errorbar, panel=panel.errorbar,
		box.ratio=3, stack=FALSE, col=cols, origin=0, 
		key=list(text=list(c("Brutes", "LC 3", "LC 3,2", "LC 3,2,1", "mb1 : LC3, maxredun : 2, minrate:8",
								"mb2: All, maxred: 2, minrate: 8",
								"mb3: All, maxred: 1, minrate: 8")), 
				rectangles=list(col=cols), x=1, y=0.98, corner=c(1, 1)),
		xlab="Type de traitement",
		ylab="Ratio de surface Argos:GPS (\261 ecart-type)",
		main="Ratio de surface Argos:GPS en fonction du filtre appliqu\u00e9",
		scales = list(col=1, tck=c(1,0)),
    addN = TRUE, nOffset=3, nText=paste("n=", hr.stats.mcp$n)
)
#dev.off()

png(file=file.path(DATA_PATH, "OverlapIndex_mcp.png"), width=700, height=700)
barchart(overlapIndex ~ type, hr.stats.mcp, groups=NULL, ly=hr.stats.mcp[, overlapIndex.lb],
		uy=hr.stats.mcp[, overlapIndex.ub], prepanel=prepanel.errorbar, panel=panel.errorbar,
		box.ratio=3, stack=FALSE, col=cols, origin=0, ylim=c(0, 70), 
		key=list(text=list(c("Brutes", "LC 3", "LC 3,2", "LC 3,2,1", "mb1 : LC3, maxredun : 2, minrate:8",
								"mb2: All, maxred: 2, minrate: 8",
								"mb3: All, maxred: 1, minrate: 8")), 
				rectangles=list(col=cols), x=1, y=0.98, corner=c(1, 1)),
		xlab="Type de traitement",
		ylab="Index de chevauchement de Dahl (\261 ecart-type)",
		main="Index de chevauchement de Dahl en fonction du filtre appliqu\u00e9",
		scales = list(col=1,tck=c(1,0))
)
dev.off()

#'#################################################################################################
#'#                                                                                              ##
#'#                                 Kernel                                                       ##
#'#                                                                                              ##
#'#################################################################################################

hr.stats[is.infinite(sizeRatio.kernel.mean), sizeRatio.kernel.mean := NA]
hr.stats.kernel <- hr.stats[, list(sizeRatio=mean(sizeRatio.kernel.mean, na.rm=TRUE), sizeRatio.sd=sd(sizeRatio.kernel.mean, na.rm=TRUE),
				overlapIndex=mean(overlapIndex.kernel.mean, na.rm=TRUE), overlapIndex.sd=sd(overlapIndex.kernel.mean, na.rm=TRUE), n=mean(n.mean)), by=list(type, h)]
hr.stats.kernel[, sizeRatio.lb := sizeRatio - sizeRatio.sd]
hr.stats.kernel[, sizeRatio.ub := sizeRatio + sizeRatio.sd]
hr.stats.kernel[, overlapIndex.lb := overlapIndex - overlapIndex.sd]
hr.stats.kernel[, overlapIndex.ub := overlapIndex + overlapIndex.sd]

#'#
#'# Graphiques
#'# 



png(file=file.path(DATA_PATH, "SizeRatio_kernel.png"), width=700, height=700)
barchart(sizeRatio ~ type, hr.stats.kernel, groups=h, ly=hr.stats.kernel[,sizeRatio.lb],
		uy=hr.stats.kernel[, sizeRatio.ub], prepanel=prepanel.errorbar, panel=panel.errorbar,
		box.ratio=3, stack=FALSE, col=cols, origin=0, 
		key=list(text=list(c("h = href", "h = 500", "h = 600", "h = 800")), 
				rectangles=list(col=cols[1:length(levels(hr.stats.all$h))]), x=1, y=0.98, corner=c(1, 1)),
		xlab="Type de traitement",
		ylab="Ratio de surface Argos:GPS (\261 ecart-type)",
		main="Ratio de surface Argos:GPS en fonction du filtre appliqu\u00e9",
		scales = list(col=1,tck=c(1,0))
)
dev.off()

png(file=file.path(DATA_PATH, "OverlapIndex_kernel.png"), width=700, height=700)
barchart(overlapIndex ~ type, hr.stats.kernel, groups=h, ly=hr.stats.kernel[, overlapIndex.lb],
		uy=hr.stats.kernel[, overlapIndex.ub], prepanel=prepanel.errorbar, panel=panel.errorbar,
		box.ratio=3, stack=FALSE, col=cols, origin=0, ylim=c(18, 100), 
		key=list(text=list(c("h = href", "h = 500", "h = 600", "h = 800")), 
				rectangles=list(col=cols[1:length(levels(hr.stats.all$h))]), x=1, y=0.98, corner=c(1, 1)),
		xlab="Type de traitement",
		ylab="Index de chevauchement de Dahl (\261 ecart-type)",
		main="Index de chevauchement de Dahl en fonction du filtre appliqu\u00e9",
		scales = list(col=1,tck=c(1,0))
)
dev.off()


write.table(hr.stats.mcp, file.path(DATA_PATH, "HRstats_mcp.txt"), row.names=FALSE)
write.table(hr.stats.kernel, file.path(DATA_PATH, "HRstats_kernel.txt"), row.names=FALSE)


#'#################################################################################################
#'#                                                                                              ##
#'#                                 Graphique comparatif                                         ##
#'#                                                                                              ##
#'#################################################################################################


hr.stats.mcp[, method := "mcp"]
hr.stats.kernel[, method := paste("kernel", h, sep=".")]
hr.stats.kernel$h <- NULL
hr.stats.all <- rbindlist(list(hr.stats.mcp, hr.stats.kernel))
hr.stats.all[, method := factor(method, levels=c("mcp", "kernel.href", "kernel.500", "kernel.600", "kernel.800"))]

png(file=file.path(DATA_PATH, "SizeRatio_all.png"), width=700, height=700)
barchart(sizeRatio ~ type, hr.stats.all , groups=method, ly=hr.stats.all[, sizeRatio.lb],
		uy=hr.stats.all[, sizeRatio.ub], prepanel=prepanel.errorbar, panel=panel.errorbar,
		box.ratio=3, stack=FALSE, col=cols, origin=0,  
		key=list(text=list(c("mcp", "kernel.href", "kernel.500", "kernel.600", "kernel.800")), 
				rectangles=list(col=cols[1:length(levels(hr.stats.all$method))]), x=1, y=0.98, corner=c(1, 1)),
		xlab="Type de traitement",
		ylab="Ratio de surface Argos:GPS (\261 ecart-type)",
		main="Ratio de surface Argos:GPS en fonction du filtre appliqu\u00e9",
		scales = list(col=1,tck=c(1,0))
)
dev.off()

png(file=file.path(DATA_PATH, "OverlapIndex_all.png"), width=700, height=700)
barchart(overlapIndex ~ type, hr.stats.all , groups=method, ly=hr.stats.all[, overlapIndex.lb],
		uy=hr.stats.all[, overlapIndex.ub], prepanel=prepanel.errorbar, panel=panel.errorbar,
		box.ratio=3, stack=FALSE, col=cols, origin=0, ylim=c(0, 100), 
		key=list(text=list(c("mcp", "kernel.href", "kernel.500", "kernel.600", "kernel.800")), 
				rectangles=list(col=cols[1:length(levels(hr.stats.all$method))]), x=1, y=0.98, corner=c(1, 1)),
		xlab="Type de traitement",
		ylab="Index de chevauchement de Dahl (\261 ecart-type)",
		main="Index de chevauchement de Dahl en fonction du filtre appliqu\u00e9",
		scales = list(col=1,tck=c(1,0))
)
dev.off()